package cn.anecansaitin.whimsy.common.item.handcannon;

import cn.anecansaitin.whimsy.client.item.render.handcannon.HandCannonArmRender;
import cn.anecansaitin.whimsy.common.ICodeObject;
import net.minecraft.client.renderer.BlockEntityWithoutLevelRenderer;
import net.minecraft.world.item.Item;
import net.neoforged.neoforge.client.extensions.common.IClientItemExtensions;
import org.jetbrains.annotations.Nullable;
import software.bernie.geckolib.animatable.GeoItem;
import software.bernie.geckolib.animatable.SingletonGeoAnimatable;
import software.bernie.geckolib.core.animatable.GeoAnimatable;
import software.bernie.geckolib.core.animatable.instance.AnimatableInstanceCache;
import software.bernie.geckolib.core.animation.AnimatableManager;
import software.bernie.geckolib.core.animation.AnimationController;
import software.bernie.geckolib.core.animation.EasingType;
import software.bernie.geckolib.core.animation.RawAnimation;
import software.bernie.geckolib.core.object.PlayState;
import software.bernie.geckolib.util.GeckoLibUtil;

import java.util.function.Consumer;

public class HandCannonArm extends Item implements GeoItem, ICodeObject {
    public HandCannonArm() {
        super(new Properties());
        SingletonGeoAnimatable.registerSyncedAnimatable(this);
    }

    @Override
    public void registerControllers(AnimatableManager.ControllerRegistrar controllers) {
        controllers.add(new AnimationController<GeoAnimatable>(this, "main", 0, (i) -> {
            AnimationController<GeoAnimatable> controller = i.getController();

            if (controller.getAnimationState() == AnimationController.State.STOPPED) {
                return PlayState.STOP;
            }

            return PlayState.CONTINUE;
        })
                .triggerableAnim("idle", IDLE)
                .triggerableAnim("shoot", SHOOT)
                .triggerableAnim("shooting", SHOOTING)
                .triggerableAnim("charging", CHARGING)
                .triggerableAnim("auxiliary", AUXILIARY)
                .triggerableAnim("auxiliary.back", AUXILIARY_BACK));
    }

    @Override
    public AnimatableInstanceCache getAnimatableInstanceCache() {
        return geoCache;
    }

    @Override
    public void initializeClient(Consumer<IClientItemExtensions> consumer) {
        consumer.accept(new IClientItemExtensions() {
            @Nullable
            private HandCannonArmRender render;

            @Override
            public BlockEntityWithoutLevelRenderer getCustomRenderer() {
                if (render == null) {
                    render = new HandCannonArmRender();
                }

                return render;
            }
        });
    }

    private static final RawAnimation IDLE = RawAnimation.begin()
            .thenPlay("idle");
    private static final RawAnimation SHOOT = RawAnimation.begin()
            .thenPlayAndHold("shoot");
    private static final RawAnimation SHOOTING = RawAnimation.begin()
            .thenPlayAndHold("shooting");
    private static final RawAnimation CHARGING = RawAnimation.begin()
            .thenPlayAndHold("charging");
    private static final RawAnimation AUXILIARY = RawAnimation.begin()
            .thenPlayAndHold("auxiliary");
    private static final RawAnimation AUXILIARY_BACK = RawAnimation.begin()
            .thenPlayAndHold("auxiliary.back");
    private final AnimatableInstanceCache geoCache = GeckoLibUtil.createInstanceCache(this);
}
